<?php

namespace apiend\modules\book\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * BookSearch is the model class for table "{{%book}}".
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookAuditSearch extends BookSearch {

    /**
     * @var string 关键字
     */
    public $keyword;

    /**
     * @var status 状态
     */
    public $statusList;

    /**
     * @var status 状态
     */
    public $sort = [
        'defaultOrder' => [
            'created_at' => SORT_DESC,
        ]
    ];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword', 'statusList'], 'safe'],
        ];
    }

    public function getActiveStatus() {
        $status = [
            0 => '未启用',
            10 => '已启用',
        ];
        return ArrayHelper::getValue($status, $this->statusList);
    }

    /**
     * Searching member apply
     * @param  array $params
     * @return \yii\data\ActiveDataProvider
     */
    public function buildQuery($params = []) {
        $query = static::find()->active([
            static::STATUS_ACTIVE,
            static::STATUS_PENDDING,
        ]);
        if (!empty($params)) {
            $query->andFilterWhere($params);
        }
        if ($this->keyword) {
            $query->andFilterWhere([
                'or',
                ['like', 'company_name', $this->keyword],
                ['like', 'contact', $this->keyword],
                ['like', 'contact_mobile', $this->keyword],
            ]);
        }
        return $query;
    }

    /**
     * {@inheritdoc}
     */
    public function fieldConfigs() {
        return [
            [
                'attribute' => 'id',
                'headerOptions' => ['width' => 80],
            ],
            [
                'attribute' => 'company_name',
                'headerOptions' => ['width' => '200'],
            ],
            [
                'label' => '参观人数',
                'attribute' => 'numbers',
                'format' => 'decimal',
                'headerOptions' => ['align' => 'right'],
                'contentOptions' => ['align' => 'right'],
            ],
            [
                'label' => '场地需求',
                'attribute' => 'room.name',
                'options' => [
                    'nullText' => '无座谈',
                ],
                'headerOptions' => ['align' => 'right'],
                'contentOptions' => ['align' => 'right'],
            ],
            [
                'attribute' => 'date',
                'options' => [
                    'nullText' => '-',
                ],
                'headerOptions' => ['align' => 'right'],
                'contentOptions' => ['align' => 'right'],
            ],
            [
                'label' => '参观时段',
                'attribute' => 'stagesText',
                'format' => 'tag',
            ],
            [
                'label' => '参观需求',
                'attribute' => 'categorysText',
                'format' => 'tag',
            ],
            [
                'class' => 'common\rest\grid\CellColumn',
                'label' => '参观联系人',
                'cellOptions' => [
                    'title' => 'contact',
                    'label' => 'contact_mobile',
                ],
                'headerOptions' => ['width' => '200'],
            ],
            [
                'attribute' => 'waiterDepartment.name',
                'visible' => false
            ],
            [
                'class' => 'common\rest\grid\CellColumn',
                'label' => '公司对接',
                'cellOptions' => [
                    'title' => 'waiter_name',
                    'label' => 'waiterDepartment.name',
                ],
                'headerOptions' => ['width' => '200'],
            ],
            [
                'class' => 'common\rest\grid\LabelColumn',
                'attribute' => 'status',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'attribute' => 'updated_at',
                'format' => 'datetimeRow',
                'headerOptions' => ['class' => 'text-right'],
                'contentOptions' => ['class' => 'text-right'],
            ],
            [
                'class' => 'common\rest\grid\ActionColumn',
                'template' => '{view:审核}',
                'headerOptions' => ['width' => '120'],
            ],
        ];
    }

}
